{% extends "template/listEdit.volt" %}

{% block title %}子系统管理{% endblock %}


{% block filter %}
    <div class="col-md-4 col-sm-6 col-xs-12">
        <table>
            <tr>
                <th>内部名称:</th>
                <td><input type="text" name="system_name" value=""></td>
            </tr>

        </table>
    </div>
    <div class="col-md-4 col-sm-6 col-xs-12">
        <table>
            <tr>
                <th>系统名称:</th>
                <td><input type="text" name="system_title" value=""></td>
            </tr>

        </table>
    </div>
    <div class="col-md-4 col-sm-6 col-xs-12">
        <table>
            <tr>
                <th>创建时间:</th>
                <td><input type="date-h" name="created_at_start" data-attr="date" id="flight-date-start"
                           value="">-<input type="date-h" name="created_at_end" data-attr="date right" class="end"
                                            value=""></td>
            </tr>

            <tr>
                <td colspan="2">
                    <div id="btn_reset_param" class="btn-0 button">清 空</div>
                    <div id="btn_search_data" class="btn-1 button">过 滤</div>
                </td>
            </tr>
        </table>
    </div>
{% endblock %}
{% block hiddenCheckAll %}hidden{% endblock %}
{% block hiddenContent %}

    <div id="roleInfo">{{ roleData }}</div>

{% endblock %}
{% block jscontent %}
    <script>
        var roleInfoData=eval("("+$("#roleInfo").text()+")");
        var currentRoleInfo=null;
        var systemId=0;
        function getRoleInfo(value){
            var data=new Array();
            for(var i=0;i<roleInfoData.length;i++){
                var item=roleInfoData[i];
                if(value&&value==1){
                    data.push({"id":item["id"],"name":item["user_role_name"]});

                }else{
                    if(item["id"]!=1){
                        data.push({"id":item["id"],"name":item["user_role_name"]});
                    }
                }
            }
            return data;

        }


        function updateSrcData(systemId,phone,resultCall){
            var request=new Object();
            request["system"]=systemId;
            if(phone&&phone.length>0){
                request["phone"]=phone;
            }

            feimaPost("/subsystem/undispatchUsers", request, function (result) {

                if(result.status==0){
                    var srcList = new Array();
                    for (var i = 0; i < result.data.length; i++) {
                        var item=result.data[i];
                        var isChecked=false;
                        var curRole=undefined;
                        if(item["user_role_id"]){
                            for(var temp=0;temp<currentRoleInfo.length;temp++){
                                if(item["user_role_id"]==currentRoleInfo[temp]["id"]){
                                    curRole=currentRoleInfo[temp];
                                    isChecked=true;
                                }
                            }
                        }
                        var nickname=item["phone"];
                        var tarNickName=item['nickname'];
                        if(tarNickName){

                            if(tarNickName.length>10){
                             tarNickName=tarNickName.substr(0,4)+"..."+tarNickName.substr(tarNickName.length-4,4);
                            }
                            nickname+="("+tarNickName+")";
                        }
                        var item = {
                            icon: "/images/group-select/group_selcet_head_icon.png",
                            uid: nickname,
                            checked:isChecked,
                            role: curRole,
                            user_id:item["id"]
                        };
                        srcList.push(item);
                    }
                    resultCall(srcList);
                }else{
                    FAlert(result.message);
                }
            });
        }


        function clickSysDispatch(value) {
            systemId=value;
            currentRoleInfo=getRoleInfo(value);
            updateSrcData(value,null,function (srcList) {

                groupSelcetM.init();
                groupSelcetM.updateRoleList(currentRoleInfo);
                groupSelcetM.updateSrcList(srcList);
                groupSelcetM.filterSrcDataCallback = function (value) {

                    updateSrcData(systemId,value,function(result){
                        groupSelcetM.updateSrcList(result);
                    });
                };
                groupSelcetM.cancelCallback = function () {

                };
                groupSelcetM.confirmCallback = function (list) {


                    if(list){

                        var updateData=new Array();
                        var deleteAll=0;
                        for(var i=0;i<list.length;i++){
                            var item=list[i];
                            updateData.push({
                                "user_id":item["user_id"],
                                "user_role_id":item["role"]["id"]
                            });
                        }
                        if(updateData.length<=0){
                            deleteAll=1;
                        }
                        $.post("/subsystem/updateBindUserSystem",{
                            "system":systemId,
                            "updateInfo":updateData,
                            "deleteAll":deleteAll
                        },function(result){

                            FAlert(result.message);
                            PageDataProxy.refresh();
                        })
                    }

                };
            });
        }

        function updatePlaneSrcData(param,resultCall){
            systemId=param["system"];
            loadingView.show();
            feimaPost("/subSystem/undispatchPlane",param,function(data){
                loadingView.remove();
                var srcList = new Array();
                var planeData=data.data;
                $.each(planeData,function(i,item){
                    var modelName=item["model_name"]?item["model_name"]:"";
                    var imageUrl=null;
                    if(modelName=="D200"){
                        imageUrl="/images/stat/small-D200.png"
                    }
                    if(modelName=="D1000"||modelName.indexOf("Phantom")>=0){
                        imageUrl="/images/stat/small-D1000.png"
                    }
                    if(modelName=="F1000"){
                        imageUrl="/images/stat/small-F1000.png"
                    }
                    if(modelName=="F200"){
                        imageUrl="/images/stat/small-F200.png"
                    }
                    if(modelName=="F300"){
                        imageUrl="/images/stat/small-F300.png"
                    }
                    var srcItem={
                        uid:item["uas_serial_id"],
                        id:item["id"],
                        checked:(systemId==item["system_id"]?1:0),
                        icon:imageUrl
                    };
                    srcList.push(srcItem);
                });
                if(resultCall){
                    resultCall(srcList);
                }
            });
        }
        //飞机分配
        function clickPlaneDispatch(sysId){
            updatePlaneSrcData({system:sysId},function(planeData){

                groupSelcetM.init({
                    srcTitle: "选择要添加的飞机",
                    destTitle: "已选择飞机",
                    selectedUnit: "架飞机",
                    comfirm: "更 新",
                    cancel: "取 消"
                }).updateSrcList(planeData);
                groupSelcetM.filterSrcDataCallback = function(value) {

                    updatePlaneSrcData(
                            {system:sysId,uas_id:value},function(data){
                                groupSelcetM.updateSrcList(data);
                            });
                };
                groupSelcetM.confirmCallback = function(list) {
                    feimaPost("/subsystem/batchAddMonitorPlane",{
                        system:systemId,
                        planes:list
                    },function(result){
                            FAlert(result.message);
                            PageDataProxy.refresh();
                    });
                };
            })
        }




        $(function () {

            PageDataProxy.init({
                "operation_style": "width: 330px; min-width: 160px;",
                "action_detail_url": "/subsystem/detail",
                "url": "/subsystem/list",
                "table_id": "#mytable",
                "page_control_id": "#data-wrap",
                "page_size_id": "#rows-perpage-select",
                "page_total_id": "#data_count_total",
                "use_check_box": true,
                "data_field_process": function (name, value) {
                    if (name == "company") {
                        return PageDataProxy.formatShortContent(value, 18);
                    }
                    if(name=="id"){
                        return "<a href='javascript:void(0)' onclick=PageDataProxy.actionDetail('id=" + value + "')>"+value+"</a>";
                    }
                    return value;
                },
                "data_field_action_process": function (tag) {
                    return "<td onclick=clickSysDispatch(" + tag + ")><img class='action-show-icon'>人员分配</td>" +
                            "<td onclick=clickPlaneDispatch(" + tag + ")><img class='action-show-icon'>飞机分配</td>" +
                            "<td onclick=PageDataProxy.actionDelete('id','" + tag + "')><img class='action-del-icon'>删除</td>";
                },
                "table_style": {
                    "id": "width-s",
                    "system_name": "width-b",
                    "created_at": "width-m",
                    "system_title": "width-m"
                },

            });


        })
    </script>
{% endblock %}